The Chinese Remainder Theorem
Introduction
In "solving linear congruences", we learned how to solve a single congruence such as $$ax \equiv b \pmod{n}.$$ But what if we have several congruences at the same time, all involving the same unknown number?
For example:
- $x \equiv 2 \pmod{3}$
- $x \equiv 3 \pmod{5}$
- $x \equiv 2 \pmod{7}$
Is there a number that satisfies all of them at once?
Surprisingly, when the moduli (the numbers after “mod”) are chosen well, there is always a solution, and that solution is unique up to a certain size. This remarkable fact is called the Chinese Remainder Theorem, named after ancient mathematicians in China who discovered it more than 1,500 years ago.
What a System of Congruences Is
A system of congruences is a collection of statements like: $$x \equiv a_1 \pmod{n_1}$$ $$x \equiv a_2 \pmod{n_2}$$ $$\dots$$ $$x \equiv a_k \pmod{n_k}.$$ Each congruence says that $x$ leaves a certain remainder when divided by a certain number.
The goal is to find a single number $x$ that satisfies all of them.
Sometimes this is easy.
Sometimes it seems impossible.
The Chinese Remainder Theorem tells us exactly when it is possible.
When a System Has a Solution
The key condition is simple:
A system of congruences has a solution if all the moduli are pairwise coprime.
This means:
- Any two moduli share no common factors except $1$.
- For example, $3$, $5$, and $7$ are pairwise coprime.
- But $4$ and $6$ are not, because they share a factor of $2$.
If the moduli are pairwise coprime, then:
- A solution exists.
- The solution is unique modulo the product of all the moduli.
Example:
- $x \equiv 2 \pmod{3}$
- $x \equiv 3 \pmod{5}$
Since $3$ and $5$ are coprime, a solution exists, and it is unique modulo $15$.
Why Solutions Are Unique
If the moduli are pairwise coprime, then the product $$N = n_1 n_2 \cdots n_k$$ acts like a “master modulus.”
The Chinese Remainder Theorem says:
- There is exactly one solution modulo $N$.
- All other solutions differ from it by multiples of $N$.
This means that once you find one solution, you have found them all.
Example:
If a system has a unique solution modulo $15$, then all solutions are: $$x = x_0 + 15k,$$ where $k$ is any whole number.
Solving Two Congruences by Inspection
Let’s start with the simplest case: $$x \equiv a \pmod{m}$$ $$x \equiv b \pmod{n}.$$ If $m$ and $n$ are coprime, we can often solve this by looking for a number that fits both patterns.
Example:
Solve:
- $x \equiv 2 \pmod{3}$
- $x \equiv 3 \pmod{5}$
List numbers that are $2$ mod $3$:
- $2, 5, 8, 11, 14, 17, \dots$
Now check which of these are $3$ mod $5$:
The first match is $8$.
So the solution is: $$x \equiv 8 \pmod{15}.$$ This method works well for small numbers.
Solving Two Congruences Using Multipliers
For larger numbers, inspection becomes slow.
A more systematic method uses the idea of multipliers.
Given: $$x \equiv a \pmod{m}$$ $$x \equiv b \pmod{n},$$ we want to build $x$ from pieces that “fit” each congruence.
Define:
- $M = mn$
- $M_m = M / m = n$
- $M_n = M / n = m$
We then find numbers:
- $y_m$ such that $M_m y_m \equiv 1 \pmod{m}$
- $y_n$ such that $M_n y_n \equiv 1 \pmod{n}$
These are just multiplicative inverses.
Then the solution is: $$x = a M_m y_m + b M_n y_n.$$ This looks complicated, but the steps are simple:
- Compute $M_m$ and $M_n$.
- Find their inverses modulo $m$ and $n$.
- Combine everything.
We will see examples in the next section.
Solving Several Congruences Step by Step
Suppose we want to solve:
- $x \equiv 2 \pmod{3}$
- $x \equiv 3 \pmod{5}$
- $x \equiv 2 \pmod{7}$
Since $3$, $5$, and $7$ are pairwise coprime, a solution exists.
Step 1: Solve the first two
From earlier, we know:
Step 2: Combine this with the third
Now solve:
- $x \equiv 8 \pmod{15}$
- $x \equiv 2 \pmod{7}$
List numbers that are $8$ mod $15$:
- $8, 23, 38, 53, 68, \dots$
Check which are $2$ mod $7$:
- $2, 9, 16, 23, 30, \dots$
The first match is $23$.
So the solution is: $$x \equiv 23 \pmod{105}.$$ This means all solutions are: $$x = 23 + 105k.$$
Solving Several Congruences Using Multiplicative Inverses
Now let’s solve the same system:
- $x \equiv 2 \pmod{3}$
- $x \equiv 3 \pmod{5}$
- $x \equiv 2 \pmod{7}$
using the general Chinese Remainder Theorem formula with multiplicative inverses.
Step 1: Compute the product of the moduli
The moduli are pairwise coprime, so we set $$N = 3 \cdot 5 \cdot 7 = 105.$$ For each congruence, define
- $N_1 = N / 3 = 35$
- $N_2 = N / 5 = 21$
- $N_3 = N / 7 = 15$
Step 2: Find the multiplicative inverses
We now find numbers $y_1, y_2, y_3$ such that
- $N_1 y_1 \equiv 1 \pmod{3}$
- $N_2 y_2 \equiv 1 \pmod{5}$
- $N_3 y_3 \equiv 1 \pmod{7})$
For $y_1$:
We need $35 y_1 \equiv 1 \pmod{3})$
Since $35 \equiv 2 \pmod{3}$, this is $$2 y_1 \equiv 1 \pmod{3}.$$ Trying small values: $y_1 = 2$ gives $2 \cdot 2 = 4 \equiv 1 \pmod{3}$, so $$y_1 = 2.$$
For $y_2$:
We need $21 y_2 \equiv 1 \pmod{5}$.
Since $21 \equiv 1 \pmod{5}$, this is $$1 \cdot y_2 \equiv 1 \pmod{5},$$ so $$y_2 = 1.$$
For $y_3$:
We need $15 y_3 \equiv 1 \pmod{7}$.
Since $15 \equiv 1 \pmod{7}$, this is $$1 \cdot y_3 \equiv 1 \pmod{7},$$ so $$y_3 = 1.$$
Step 3: Build the solution
The Chinese Remainder Theorem tells us that a solution is given by $$x = a_1 N_1 y_1 + a_2 N_2 y_2 + a_3 N_3 y_3,$$ where $a_1, a_2, a_3$ are the remainders:
- $a_1 = 2 \pmod {3}$
- $a_2 = 3 \pmod {5}$
- $a_3 = 2 \pmod {7}$.
So $$x = 2 \cdot 35 \cdot 2 + 3 \cdot 21 \cdot 1 + 2 \cdot 15 \cdot 1.$$ Compute each term:
- $2 \cdot 35 \cdot 2 = 140$
- $3 \cdot 21 \cdot 1 = 63$
- $2 \cdot 15 \cdot 1 = 30$
so $$x = 140 + 63 + 30 = 233.$$ We now reduce $233$ modulo $N = 105$: $$233 - 2 \cdot 105 = 233 - 210 = 23.$$ So $$x \equiv 23 \pmod{105}.$$ This matches the solution we found earlier by combining congruences step by step:
- Final answer: $x \equiv 23 \pmod{105}$.
Why the Chinese Remainder Theorem Works (Gentle Explanation)
The key idea is that when the moduli are coprime, each congruence controls a different “aspect” of the number.
For example:
- Modulo $3$ tells you the remainder when dividing by $3$.
- Modulo $5$ tells you the remainder when dividing by $5$.
Since $3$ and $5$ share no factors, these two pieces of information do not interfere with each other.
It is like knowing:
- The hour on a 12-hour clock
- The minute on a 60-minute clock
These two pieces of information uniquely determine the time.
The Chinese Remainder Theorem says that the same idea works for any number of moduli, as long as they are pairwise coprime.
Calculator
Solving systems of linear congruences
- Given a system of linear congruences such as:
- $x \equiv a_0 \pmod{m_0}$
- $x \equiv a_1 \pmod{m_1}$
- $x \equiv a_2 \pmod{m_2}$
- We can encode this an array of arrays
solveLinearCongruenceSystem([[a0, m0], [a1, m1],[a2, m2]]) solveLinearCongruenceSystem([[2, 3], [3, 5],[2, 7]])
Exercises
Solve each system of congruences or explain why no solution exists.
- $x \equiv 1 \pmod{3}$
- $x \equiv 2 \pmod{5}$
- $x \equiv 2 \pmod{4}$
- $x \equiv 3 \pmod{6}$
- $x \equiv 3 \pmod{7}$
- $x \equiv 4 \pmod{9}$
- $x \equiv 2 \pmod{5}$
- $x \equiv 3 \pmod{7}$
- $x \equiv 1 \pmod{3}$
- $x \equiv 4 \pmod{11}$
- $x \equiv 5 \pmod{13}$
- $x \equiv 6 \pmod{8}$
- $x \equiv 1 \pmod{3}$
- $x \equiv 0 \pmod{4}$
- $x \equiv 1 \pmod{5}$
- $x \equiv 2 \pmod{7}$
- $x \equiv 3 \pmod{10}$
- $x \equiv 4 \pmod{9}$
- $x \equiv 1 \pmod{6}$
- $x \equiv 2 \pmod{7}$
- $x \equiv 3 \pmod{8}$
- $x \equiv 5 \pmod{12}$
- $x \equiv 7 \pmod{25}$